#include <stdio.h>
#include <string.h>

void getname(char *filename, char *name){
    char tmp_filename[32];
    char *delim = ".", *ptr, *saveptr;
    strcpy(tmp_filename, filename);
    ptr = strtok_r(tmp_filename, delim, &saveptr);
    strcpy(name, ptr);
}
void createHeaderFile(char *filename){
    char name[32];
    char header_file[32] = {0};

    getname(filename, name);
    sprintf(header_file, "%s.h", name);
    FILE *fp = fopen(header_file, "w");

    fprintf(fp, "#ifndef __%s_h__\n", name);
    fprintf(fp, "#define __%s_h__ 1\n", name);
    fprintf(fp, "extern char %s_carray[];\n", name);
    fprintf(fp, "extern char *%s_filename;\n", name);
    fprintf(fp, "extern int %s_len;\n", name);
    fprintf(fp, "#endif\n");
    fclose(fp);
}
void createCFile(char *filename){
    int c = 0, len = 0;
    char c_filename[32], name[32];

    getname(filename, name);
    sprintf(c_filename, "%s.c", name);

    FILE *fp = fopen(filename, "rb");
    FILE *w_fp = fopen(c_filename, "w");
    fprintf(w_fp, "#include \"%s.h\"\n", name);
    fprintf(w_fp, "char *%s_filename=\"%s\";\n", name, filename);
    fprintf(w_fp, "char %s_carray[]={", name);
    while((c = fgetc(fp)) != EOF){
        fprintf(w_fp, "0x%x,", c);
        len++;
    }
    fprintf(w_fp, "};\n");
    fprintf(w_fp, "int %s_len=%d;\n", name, len);
    fclose(fp);
    fclose(w_fp);
}
void transFile2Carray(char *filename){

    createHeaderFile(filename);
    createCFile(filename);
   
}

int main(int argc, char **argv){
    char buffer[64] = {0};
    FILE *fp = popen("ls | grep .sh", "r");

    while(fgets(buffer, sizeof(buffer), fp)){
        buffer[strlen(buffer) - 1] = 0;
        transFile2Carray(buffer);
    }
}
